package edu.rit.hvz.android;

import edu.rit.hvz.android.DataLayer.PlayerDBAdapter;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class SubmitTag extends Activity implements OnClickListener {
	private EditText zombieID;
	private EditText humanID;
	
	private PlayerDBAdapter db;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.onActivityCreateSetTheme(this);
        setContentView(R.layout.submit_tag_layout);
        
        db = new PlayerDBAdapter(this);
        
        zombieID = (EditText) findViewById(R.id.editTextZombieID);
        humanID = (EditText) findViewById(R.id.editTextHumanID);
        
		findViewById(R.id.submitTagButton).setOnClickListener(this);
		((EditText)findViewById(R.id.editTextHumanID)).setOnEditorActionListener(new OnEditorActionListener(){
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				onClick(v);
				return true;
			}			
		});
    }

	public void onClick(View v) {
		
		long humanIDLong, zombieIDLong;
		try{
			humanIDLong = Long.parseLong(humanID.getText().toString());			
		} catch (NumberFormatException e){
			Toast.makeText(this, "Invalid Human ID" , Toast.LENGTH_SHORT).show();
			return;
		}
		try{
			zombieIDLong = Long.parseLong(zombieID.getText().toString());		
		} catch (NumberFormatException e){
			Toast.makeText(this, "Invalid Zombie ID" , Toast.LENGTH_SHORT).show();
			return;
		}			
		
		
		boolean result = db.submitKill(zombieIDLong, humanIDLong);
		if( result ){
			Toast.makeText(this, "Tag Submitted" , Toast.LENGTH_SHORT).show();
			((EditText)findViewById(R.id.editTextHumanID)).setText("");
		} else {
			Toast.makeText(this, "Invalid ID" , Toast.LENGTH_SHORT).show();
		}
	}
	
}
